//
//  MainViewController.m
//  ZhumuSDKSample
//
//  Copyright 2006-2015 SUIRUI Co., Ltd. All rights reserved.
//

#import "MainViewController.h"
#import "CallCenter.h"
#import "SettingsViewController.h"
#import "UIImage+Additions.h"
#import "UIColor+Additions.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>

//测试账号: 呼叫前需要发送的请求信息
//#define kAPI_KEY  @"O8czzSQBEWkq0sHDk8PixUc9FdY27"
//#define kAPI_SECRET @"YgAfm0VURjiCeYgAfm0V=="
//#define kLOGIN_TYPE 3 //代表email类型
#define kLOGIN_NAME @"598837443@qq.com"
//#define kDISPLAY_NAME @"测试账号"


#define kAPI_KEY  @"O8czzSQBEWkq0sHDk8PixUc9FdY27"
#define kAPI_SECRET @"YgAfm0VURjiCeYgAfm0V=="
//账户登录类型:1-Zcode, 2-Mobile, 3-Email
#define kLOGIN_TYPE 3 //代表email类型
//根据相应的LOGIN_TYPE,所对应的账号名
//#define kLOGIN_NAME @"925769607@qq.com"
#define kDISPLAY_NAME @"123456"



@interface MainViewController ()<UIAlertViewDelegate>

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    // Do any additional setup after loading the view from its nib.
    self.title = @"ZhumuSDK Sample";
     
    _statusLabel.text = NSLocalizedString(@"Hello Zhumu SDK", @"");
    
    _logoImageView.image = [UIImage imageNamed:@"zhumu_chat_placeholder"];
    
    [_settingsButton setImage:[UIImage imageNamed:@"icon_settings"] forState:UIControlStateNormal];
    
    
    [_meetNowButton setTitle:NSLocalizedString(@"Meet Now", @"") forState:UIControlStateNormal];
    [_joinMeetButton setTitle:NSLocalizedString(@"Join a Meeting", @"") forState:UIControlStateNormal];
    _meetNowButton.layer.cornerRadius = 5.f;
    _joinMeetButton.layer.cornerRadius = 5.f;

#if 0
    //自定义邀请界面
    [ZhumuSDKInviteHelper sharedInstance].customizeInvite = YES;
    //[ZhumuSDKInviteHelper sharedInstance].inviteVCName = @"InviteViewController";
#endif
    
    //是否启用 Copy URL
    //[ZhumuSDKInviteHelper sharedInstance].disableCopyURL = YES;
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onDone:)];
    [self.navigationItem setLeftBarButtonItem:closeItem];

}

- (void)onDone:(id)sender
{
    UIViewController *vc = objc_getAssociatedObject(self, &rootKey);
    [[UIApplication sharedApplication] keyWindow].rootViewController = _rootVC;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    //for bug that there exist 20 pixels in the bottom while leaving meeting quickly
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

- (IBAction)onMeetNow:(id)sender
{
    [_waitView setHidden:NO];
    
    __block MainViewController *weakself = self;
    __block UIView *waitView = _waitView;
    __block RequestMeeting *info = [[RequestMeeting alloc] initWithKey:kAPI_KEY secret:kAPI_SECRET type:kLOGIN_TYPE name:kLOGIN_NAME];
    
    //即时会议: Instant_CallType
    //预约会议: Scheduled_CallType
    [[CallCenter sharedInsance] startCallWithType:Instant_CallType info:info delegate:weakself success:^(Meeting *meeting) {
        
        //success:
        //NSLog(@"%@",meeting);
        [waitView setHidden:YES];
        
    } failure:^(NSString *error) {
        
        //failure:
        [waitView setHidden:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hint", @"") message:[NSString stringWithFormat:NSLocalizedString(@"Failure: %@", @""),error] delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
        [alert show];
        
    }];
}

- (IBAction)onJoinaMeeting:(id)sender
{
    ZhumuSDKMeetingService *ms = [[ZhumuSDK sharedSDK] getMeetingService];
    if (ms)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"Please input the meeting number", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"") otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
        
        alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alert textFieldAtIndex:0].placeholder = @"#########";
        [alert textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;
        
        [alert show];
    }
}

- (IBAction)onSettings:(id)sender
{
    ZhumuSDKMeetingSettings *settings = [[ZhumuSDK sharedSDK] getMeetingSettings];
    if (!settings)
        return;

    SettingsViewController *vc = [[SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    
    [self presentViewController:nav animated:YES completion:NULL];

}

#pragma mark - Meeting Service Delegate

- (void)onMeetingReturn:(ZhumuSDKMeetError)error internalError:(NSInteger)internalError
{
    NSLog(@"onMeetingReturn:%d, internalError:%zd", error, internalError);
}

- (void)onMeetingStateChange:(ZhumuSDKMeetingState)state
{
    NSLog(@"onMeetingStateChange:%d", state);
    
#if 1
    if (state == ZhumuSDKMeetingState_InMeeting)
    {
        //自定义邀请SMS内容
        NSString *meetingID = [[ZhumuSDKInviteHelper sharedInstance] meetingID];
        NSString *smsMessage = [NSString stringWithFormat:NSLocalizedString(@"Please join meeting with ID: %@", @""), meetingID];
        [[ZhumuSDKInviteHelper sharedInstance] setInviteSMS:smsMessage];
        
        //自定义Copy URL内容
        NSString *joinURL = [[ZhumuSDKInviteHelper sharedInstance] joinMeetingURL];
        NSString *copyURLMsg = [NSString stringWithFormat:NSLocalizedString(@"Meeting URL: %@", @""), joinURL];
        [[ZhumuSDKInviteHelper sharedInstance] setInviteCopyURL:copyURLMsg];
    }
#endif
}

#pragma mark - AlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        NSString *meetNumber = [alertView textFieldAtIndex:0].text;
        if ([meetNumber length] > 0) {
            __block MainViewController *weakself = self;
            __block UIView *waitView = _waitView;
            __block MeetingInfo *info = [[MeetingInfo alloc] initWithName:kDISPLAY_NAME meetingID:meetNumber];
            [[CallCenter sharedInsance] startCallWithType:Join_CallType info:info delegate:weakself success:^(Meeting *meeting) {
                [waitView setHidden:YES];
            } failure:^(NSString *error) {
                
                [waitView setHidden:YES];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hint", @"") message:[NSString stringWithFormat:NSLocalizedString(@"Failure: %@", @""),error] delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
                [alert show];
                
            }];
        }
    }
}

@end
